package cn.factorybox.item.service.impl;

import cn.factorybox.common.utils.BeanMapUtils;
import cn.factorybox.common.utils.UUIDUtil;
import cn.factorybox.item.mapper.PersonMapper;
import cn.factorybox.item.service.PersonService;
import cn.factorybox.model.entity.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author flypig
 * @version 1.0
 * @date 2020-05-30 18:35
 */

@Service
public class PersonServiceImpl implements PersonService {

    @Autowired
    private PersonMapper personMapper;

    @Override
    public Map<String, Object> selectListByDeviceID(String deviceID) {
        Map<String,Object> map = new HashMap<>();
        List<Person> list = personMapper.selectListByDeviceID(deviceID);
        List<Map<String,Object>> data = new ArrayList<>();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月");
        for(Person person : list) {
            Map<String,Object> personMap = BeanMapUtils.beanToMap(person);
            personMap.remove("deviceID");
            personMap.put("birthday",sdf1.format(person.getBirthday()));
            personMap.put("createTime",sdf2.format(person.getCreateTime()));
            data.add(personMap);
        }
        map.put("data",data);
        return  map;
    }

    @Override
    public int insert(Map<String, Object> map) {
        String id = UUIDUtil.uuid();
        map.put("id",id);
        map.put("createTime",new Date());
        return personMapper.insert(map);
    }

    @Override
    public int update(Map<String, Object> map) {
        return personMapper.update(map);
    }

    @Override
    public Map<String, Object> selectListById(String id) {
        Map<String,Object> map = new HashMap<>();
        Person person = personMapper.selectListById(id);
        Map<String,Object> personMap = BeanMapUtils.beanToMap(person);
        personMap.remove("deviceID");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月");
        personMap.put("birthday",sdf1.format(person.getBirthday()));
        personMap.put("createTime",sdf2.format(person.getCreateTime()));
        map.put("data",personMap);
        return  map;
    }
}
